﻿using Hotel.Application.Admins;
using Hotel.Application.Admins.Dto;
using Hotel.Web.Models;
using Lazy.Captcha.Core;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;

namespace Hotel.Web.Controllers
{
    [Authorize]
    public class HomeController : Controller
    {
        private readonly IDistributedCache cache;
        private readonly IAdminService service;
        private readonly ICaptcha captcha;
        private readonly ILogger<HomeController> logger;

        public HomeController(IDistributedCache cache, IAdminService service, ICaptcha captcha, ILogger<HomeController> logger)
        {
            this.cache = cache;
            this.service = service;
            this.captcha = captcha;
            this.logger = logger;
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult Login()
        {
            logger.LogInformation("++++++++++++++++++++");
            return View();
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [HttpPost]
        [AllowAnonymous]
        public async Task<IActionResult> Login(LoginDto dto)
        {            
            return Json(await service.LoginAsync(dto));
        }

        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        public IActionResult ApiTest()
        {
            return View(new MvcModel { AddTime = DateTime.Now });
        }

        
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="dto"></param>
        /// <returns></returns>
        [AllowAnonymous]
        [HttpPost]
        public IActionResult ApiTest(MvcModel d)
        {
            return View();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        [AllowAnonymous]
        public IActionResult Captcha()
        {
            string guid = $"captcha:{Guid.NewGuid().ToString("N")}";
            var info = captcha.Generate(guid);
            HttpContext.Response.Cookies.Append("ValidateCode", guid);
            var stream = new MemoryStream(info.Bytes);
            return File(stream, "image/gif");
        }

        /// <summary>
        /// 退出登录
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public async Task<IActionResult> Logout()
        {
            await HttpContext.SignOutAsync();
            return RedirectToAction("Login");
        }

        /// <summary>
        /// 获取当前用户信息
        /// </summary>
        /// <returns></returns>
        public IActionResult GetLoginInfo()
        {
            return View();
        }

        /// <summary>
        /// 登录后默认首页
        /// </summary>
        /// <returns></returns>
        public IActionResult Index()
        {
            return View();
        }

        /// <summary>
        /// 右侧页面
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public IActionResult Right()
        {
            return View();
        }
    }
}
